package com.lwf.thymeleafdemo1.redisTest.controller;


import com.lwf.thymeleafdemo1.mysqlTest.entity.PsnInfoBVO;
import com.lwf.thymeleafdemo1.redisTest.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @version v1.0
 * @Description:java练习
 * @Author 李文峰
 * @Date 2023/7/23 0023 16:48
 */
@RestController
@RequestMapping("/lwf")
public class RedisController {

    //引入 redis
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Resource(name = "departmentService")
    private DepartmentService departmentService;

    /**
     * 存储
     *
     * @return
     */
    @RequestMapping("/set")
    public String setRedis() {
        //存储 key-value 键值对: "username"-"jaychou"
        stringRedisTemplate.opsForValue().set("username", "jaychou");
        return "redis 存储成功！";
    }

    /**
     * 读取
     *
     * @return
     */
    @GetMapping("/get")
    public String getRedis() {
        //通过 key 值读取 value
        String result = stringRedisTemplate.opsForValue().get("username");
        return result;
    }

    @GetMapping("/get1")
    public PsnInfoBVO findAll(@RequestParam String psnNo) {
        PsnInfoBVO psnInfoBVO = departmentService.findAll(psnNo);
        return psnInfoBVO;
    }
    /**
     * 存储
     *
     * @return
     */
    @GetMapping("/setNatapp")
    public String setRedisNatapp(String key,String value) {
        //存储 key-value 键值对: "key"-"value"
        stringRedisTemplate.opsForValue().set(key, value);
        return "redis 存储成功！";
    }

    /**
     * 读取
     *
     * @return
     */
    @GetMapping("/getNatapp")
    public String getRedisNatapp(String key) {
        //通过 key 值读取 value
        String result = stringRedisTemplate.opsForValue().get(key);
        return result;
    }
}
